import sys

sys.path.append("/home/zxh/otu_classifier/")
from src.datas.segmentationLabelsAndSample import labelAndSample
import tensorflow as tf
import pickle
import numpy as np
from sklearn.metrics import accuracy_score, confusion_matrix

from src.config import config, params
from src.datas import OtuData
import pandas as pd

hps = params.get_default_params()

tf.reset_default_graph()
init = tf.global_variables_initializer()

X_test, result_sample_id = pickle.load(open(config.production_pkl, 'rb'))

# X_test, y_test = pickle.load(open(config.val_2_pkl, 'rb'))
# y_test_lables, result_sample_id = labelAndSample(y_test)

test_dataset = OtuData.OtuData(
    X_test, result_sample_id=result_sample_id)

y_predict = []
test_sample_id_list = []
with tf.Session() as sess:
    sess.run(init)
    latest_file = tf.train.latest_checkpoint(config.latest_model_path)
    saver = tf.train.import_meta_graph(latest_file + '.meta')
    saver.restore(sess, latest_file)

    y_probability = sess.graph.get_tensor_by_name(
        "fully_connected_11/Relu:0")  # fully_connected_11/Relu:0 fc/fc2/BiasAdd:0
    y_pred = sess.graph.get_tensor_by_name("y_pred_model:0")

    for i in range(X_test.shape[0]):
        test_inputs, _, test_sample_id = test_dataset.next_batch(1)
        test_inputs = test_inputs + np.zeros(
            (hps.batch_size, test_inputs.shape[1], test_inputs.shape[2], test_inputs.shape[3]), dtype="float32")
        y_pred_val, y_probability_val = sess.run([y_pred, y_probability],
                                                 feed_dict={"inputs:0": test_inputs,
                                                            "keep_prob:0": params.test_keep_prob_value,
                                                            "is_training:0": False})
        print(
            "{}\t{}\t{}\t{}\t{}".format(test_sample_id[0], y_probability_val[0][0], y_probability_val[0][1],y_probability_val[0][2], y_pred_val[0]))
